﻿using System;
using System.Collections.Generic;
using System.Configuration;
using Ks.Fs.Entity;
using Ks.Fs.Core.Data.Provider;
using System.Data;
using Ks.Fs.Core.Data.SqlDataProvider.Mapper;


namespace Ks.Fs.Core.Data.SqlDataProvider
{
    public partial class SqlDataProvider : IDataProvider
    {
        public int SaveOrUpdateFolder(Folder obj)
        {
            string query = "Folder_SaveOrUpdate";
            object _id = DataHelper.ExecuteScalar(
                query, obj.Id, obj.FolderName, obj.Path, obj.Owner, obj.IsPublic,obj.ParentId
                );
            if (_id != null) return Convert.ToInt32(_id);
            return -1;
        }

        public bool DeleteFolder(int FolderId)
        {
            string query = "Folder_Delete";
            int rowAffected = DataHelper.ExecuteNonQuery(query, FolderId);
            if (rowAffected > 0) return true;
            return false;
        }

        public List<Folder> GetFolders(int ParentId, string Owner, int SortBy, bool IsSortAscending, int PageSize, int PageIndex)
        {
            string direction = "ASC";
            if (!IsSortAscending)
            {
                direction = "DESC";
            }
            IDataReader reader = DataHelper.ExecuteReader("Folder_GetByVaryParam", 
                ParentId,Owner, SortBy, direction, PageSize, PageIndex);
            FolderMapper map = new FolderMapper();
            return map.Map(reader);
        }

        public Folder GetFolder(int FolderId)
        {
            IDataReader reader = DataHelper.ExecuteReader("Folder_GetById", FolderId);
            FolderMapper map = new FolderMapper();
            List<Folder> lst = map.Map(reader);
            if (lst.Count > 0) return lst[0];
            return null;
        }
    }
}

